package lib

import (
	"algorithms/ds"
)

func init() {
	Probs = append(Probs, Problem{
		Num:         24,
		Discription: "两两交换链表中的节点",
		Level:       2,
		Labels: map[string]int{
			"链表":    1,
			"虚拟头指针": 1,
		},
	})
}

//两两一组，记录pre (cur next) nextCur
func SwapPairs(head *ds.ListNode) *ds.ListNode {
	preHead := &ds.ListNode{}
	temp := preHead
	temp.Next = head
	for {
		next1 := temp.Next
		if next1 == nil {
			break
		}

		next2 := next1.Next
		if next2 == nil {
			break
		}

		next3 := next2.Next
		temp.Next = next2
		next2.Next = next1
		next1.Next = next3
		temp = next1
	}

	return preHead.Next
}
